package GreedyAlgorithmPackage;

import org.junit.jupiter.api.Test;

/**
 * @author Lzm
 * @version 1.0
 */
/*一旦连续和为负数就要丢弃掉连续和 从下一个数重新开始*/
public class maxSubArray_ {

  @Test
  public void maxSubArrayTest(){

  }

  public int maxSubArray(int[] nums) {
    if (nums.length == 1){
      return nums[0];
    }
    int count = 0;
    int sum = Integer.MIN_VALUE;
    for (int i = 0 ;i < nums.length;i++){
        count += nums[i];
        /*当下一个count + nums[pointer] < count时就终止*/
        sum = Math.max(sum,count);
        if (count < 0){
          count = nums[i];
        }
      }
    return sum;
  }
}
